package controller

import (
	"first_try/common"
	"first_try/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"strconv"
)

//找到所有仓库信息，并传回到网页上
func E_Set_All_Storehouse(c *gin.Context) {
	DB := common.GetDB()
	var all_storage []model.Storehouse //结构体数组
	//显示所有仓库信息
	result := DB.Table("storehouse").Find(&all_storage)
	if result != nil {
		fmt.Println("!!!")
	}
	fmt.Println(all_storage)
	c.JSON(http.StatusOK, gin.H{
		"code":       "200",
		"storehouse": all_storage,
		"msg":        "成功",
	})
}

//查找某一指定仓库信息（网页用）
func E_Get_Storehouse(c *gin.Context) {
	DB := common.GetDB()
	var test model.Storehouse
	var test2 []model.Storehouse
	request := make(map[string]interface{}) //新建一个map类型的变量request，保持前端传来的数据
	//获取参数
	fmt.Println(request)
	c.ShouldBind(&request)
	fmt.Println(request)
	test.Storehouse_id = request["id"].(string) //取得相应的map值 放到结构体上
	//在数据中查询仓库是否存在
	DB.Where("Storehouse_id = ?", test.Storehouse_id).First(&test2)
	if test2[0].Storehouse_id != "" {
		fmt.Println("成功找到该仓库")
		c.JSON(200, gin.H{
			"code":       "200",
			"storehouse": test2,
			"msg":        "成功",
		})
	} else {
		c.JSON(404, gin.H{
			"code":       "404",
			"storehouse": test2,
			"msg":        "no",
		})
		fmt.Println("未找到该仓库")
	}
}

//增加仓库(网页用)
func E_Add_Storehouse(c *gin.Context) {
	DB := common.GetDB()
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	var test model.Storehouse
	test.Storehouse_id = request["Storehouse_id"].(string)
	test.Storehouse_piece, _ = strconv.Atoi(request["Storehouse_piece"].(string))
	test.Storehouse_ton = request["Storehouse_ton"].(float32)

	fmt.Println(request)
	//获取到新增仓库的所有信息
	if Insert_Storehouse(DB, test) {
		//没有找到，可以新建该仓库
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "添加成功",
		})
	} else {
		//能找到，就不可以新建该仓库
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("添加失败")
	}

}

//删除指定仓库（网页用）
func E_Delete_Storehouse(c *gin.Context) {
	DB := common.GetDB()
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	var test model.Storehouse
	test.Storehouse_id = request["good_id"].(string) //获取前端要删除的商品id
	if Delete_Storehouse(DB, test) {
		//删除成功
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "删除成功",
		})
		fmt.Println("删除成功")
	} else {
		//删除失败
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "删除成功",
		})
		fmt.Println("删除失败")
	}
}

//修改指定商品信息（网页用）
func E_Modify_Storehouse(c *gin.Context) {
	DB := common.GetDB()
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	var test model.Storehouse
	test.Storehouse_id = request["Storehouse_id"].(string)
	test.Storehouse_piece, _ = strconv.Atoi(request["Storehouse_piece"].(string))
	test.Storehouse_ton = request["Storehouse_ton"].(float32)
	test.Storehouse_company_id = request["Storehouse_company_id"].(string)
	test.Storehouse_order_id = request["Storehouse_order_id"].(string)

	//获取到全部修改信息
	if Modify_Storehouse(DB, test) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "删除成功",
		})
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "删除失败",
		})
	}
}

//查询所建的仓库是否存在
func Find_Storehouse(db *gorm.DB, test model.Storehouse) bool {
	var find_storehouse model.Storehouse
	db.Where("Storehouse_id = ?", test.Storehouse_id).First(&find_storehouse)
	if find_storehouse.Storehouse_id != "" {
		fmt.Println(find_storehouse.Storehouse_id)
		return true
	}
	return false
}

//添加仓库
func Insert_Storehouse(db *gorm.DB, test model.Storehouse) bool {
	if Find_Storehouse(db, test) {
		fmt.Println("该仓库已经存在！")
		return false
	} else {
		db.Create(&test)
		fmt.Println("添加成功")
		return true
	}
}

//删除仓库
func Delete_Storehouse(db *gorm.DB, test model.Storehouse) bool {
	if Find_Storehouse(db, test) {
		fmt.Println("已经找到该仓库")
		db.Where("Storehouse_id = ?", test.Storehouse_id).Delete(&test)
		return true
	} else {
		fmt.Println("该仓库不存在")
	}
	return false
}

//修改仓库
func Modify_Storehouse(db *gorm.DB, test model.Storehouse) bool {
	if Find_Storehouse(db, test) {
		fmt.Println("已经找到该仓库")
		db.Model(&test).Where("Storehouse_id = ?", test.Storehouse_id).Updates(
			model.Storehouse{
				Storehouse_id:         test.Storehouse_id,
				Storehouse_piece:      test.Storehouse_piece,
				Storehouse_ton:        test.Storehouse_ton,
				Storehouse_company_id: test.Storehouse_company_id,
				Storehouse_order_id:   test.Storehouse_order_id,
			},
		)
		fmt.Println("修改完成")
		return true
	} else {
		fmt.Println("并不存在该仓库")
	}
	return false
}
